package com.hq.ims.data.event.listener;

import com.hq.ims.data.entityenum.EnumPlatformTodoType;
import com.hq.ims.data.entityenum.EnumUserTodoType;
import com.hq.ims.data.event.BusinfoEvent;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.service.BasePlatformTodoStatisticsService;
import com.hq.ims.data.service.BaseUserService;
import com.hq.ims.data.service.BaseUserTodoStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * 用户操作事件监听器
 *
 * @date 2022/6/7 15:41
 */
@Component
@Slf4j
public class BusinfoListener {

    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BasePlatformTodoStatisticsService basePlatformTodoStatisticsService;
    @Resource
    private BaseUserTodoStatisticsService baseUserTodoStatisticsService;

    @Async
    @TransactionalEventListener
    public void onApplicationEvent(BusinfoEvent businfoEvent) {
        String eventType = businfoEvent.getEventType();

        switch (eventType) {
            case BusinfoEvent.ADD_EVENT:
                this.addEventHandler(businfoEvent);
                break;
            case BusinfoEvent.EDIT_EVENT:
                this.editEventHandler(businfoEvent);
                break;
            case BusinfoEvent.DEL_EVENT:
                this.delEventHandler(businfoEvent);
                break;
            case BusinfoEvent.PLATFORM_AUDIT_PASS_EVENT:
                this.platformAuditPassEventHandler(businfoEvent);
                break;
            case BusinfoEvent.PLATFORM_AUDIT_NOT_PASS_EVENT:
                this.platformAuditNotPassEventHandler(businfoEvent);
                break;
            case BusinfoEvent.CANCEL_BUSINFO_AUDIT:
                this.cancelBusinfoAuditEventHandler(businfoEvent);
                break;
            default:
                break;
        }
    }

    /**
     * 添加事件处理
     */
    public void addEventHandler(BusinfoEvent businfoEvent) {
        //平台端审核+1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoWaitAuditCount,
                1,
                businfoEvent.getBusiInfo().getBusiInfoId()
        );
    }

    /**
     * 编辑处理
     */
    public void editEventHandler(BusinfoEvent businfoEvent) {
        //平台端审核+1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoWaitAuditCount,
                1,
                businfoEvent.getBusiInfo().getBusiInfoId()
        );
    }

    /**
     * 编辑处理
     */
    public void cancelBusinfoAuditEventHandler(BusinfoEvent businfoEvent) {
        //平台端审核-1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoWaitAuditCount,
                -1,
                businfoEvent.getBusiInfo().getBusiInfoId()
        );
    }

    /**
     * 册除处理
     */
    public void delEventHandler(BusinfoEvent businfoEvent) {
        //平台端审核 - 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoWaitAuditCount,
                -1,
                businfoEvent.getBusiInfo().getBusiInfoId()
        );
        //用户端审核待办 - 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.BusinfoAuditNotifyCount,
                -1,
                businfoEvent.getBusiInfo().getBusiInfoId(),
                businfoEvent.getBusiInfo().getPublisherId(),
                businfoEvent.getBusiInfo().getOrgId()
        );
    }

    /**
     * 平台审核通过处理
     */
    public void platformAuditPassEventHandler(BusinfoEvent businfoEvent) {
        //平台端审核 - 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoWaitAuditCount,
                -1,
                businfoEvent.getBusiInfo().getBusiInfoId()
        );
        //用户端发布经营信息 + 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.BusinfoAuditNotifyCount,
                1,
                businfoEvent.getBusiInfo().getBusiInfoId(),
                businfoEvent.getBusiInfo().getPublisherId(),
                businfoEvent.getBusiInfo().getOrgId()
        );
    }

    /**
     * 平台审核不通过处理
     */
    public void platformAuditNotPassEventHandler(BusinfoEvent businfoEvent) {
        //平台端审核 - 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoWaitAuditCount,
                -1,
                businfoEvent.getBusiInfo().getBusiInfoId()
        );
        //用户端发布经营信息 + 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.BusinfoAuditNotifyCount,
                1,
                businfoEvent.getBusiInfo().getBusiInfoId(),
                businfoEvent.getBusiInfo().getPublisherId(),
                businfoEvent.getBusiInfo().getOrgId()
        );
    }


}
